﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;
using System.Xml.Linq;

namespace CH.Easy.OpenLogin
{
    public class Common
    {
        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP()
        {
            string result = String.Empty;

            if (HttpContext.Current != null && HttpContext.Current.Request != null && HttpContext.Current.Request.ServerVariables != null)
            {
                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }

                if (null == result || result == String.Empty)
                {
                    return "211.0.0.1";
                }
            }

            return result;

        }

        public static string GetConfig(string key)
        {

            if (string.IsNullOrEmpty(key))
                return "";

            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + @"config\open.config";
            var value = "";
            try
            {
                XDocument xml = xml = XDocument.Load(xmlPath);
                var configs = xml.Element("configuration").Element("appSettings");
                value = (from p in configs.Elements("add") where p.Attribute("key").Value == key select p).First().Attribute("value").Value.ToString();
            }
            catch (Exception ex)
            {

                return "";

            }
            return value;
        }
    }

    public static class OpenExtend
    {
        //json反序列化
        public static ResultDTO JsonToObject(this string str, OpenType type)
        {
            ResultDTO dto = new ResultDTO();
            dto.open_type = type;
            dto.result_string = str;
            if (string.IsNullOrEmpty(str))
                return dto;

            if (str.Contains("=") && str.Contains("&"))
            {
                dto.is_json = false;
            }
            else
            {

                dto.result = JsonConvert.DeserializeObject(str) as JObject;

            }
            return dto;
        }
        //根据文件名获取文件类型
        public static string GetContentType(this string fileName)
        {
            string contentType = "application/octetstream";
            string ext = Path.GetExtension(fileName).ToLower();
            RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(ext);

            if (registryKey != null && registryKey.GetValue("Content Type") != null)
            {
                contentType = registryKey.GetValue("Content Type").ToString();
            }

            return contentType;
        }

        /// <summary>
        /// 把集合转化为查询参数字符串
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static string GetQueryString(this Dictionary<string, string> dic)
        {
            string queryString = string.Empty;
            if (dic != null && dic.Count > 0)
            {
                foreach (KeyValuePair<string, string> item in dic)
                {
                    queryString += item.Key + "=" + item.Value + "&";
                }

                queryString = queryString.Substring(0, queryString.Length - 1);
            }
            return queryString;

        }

        /// <summary>
        /// 把字符串转化为参数集合
        /// </summary>
        /// <param name="queryString"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetParam(this string queryString)
        {
            if (queryString.StartsWith("?"))
            {
                queryString = queryString.Remove(0, 1);
            }

            Dictionary<string, string> result = new Dictionary<string, string>();

            if (!string.IsNullOrEmpty(queryString))
            {
                string[] p = queryString.Split('&');
                foreach (string s in p)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        if (s.IndexOf('=') > -1)
                        {
                            string[] temp = s.Split('=');
                            result.Add(temp[0], temp[1]);
                        }
                    }
                }
            }

            return result;
        }



    }


    public enum OpenType
    {
        Default = 1,
        QZone = 2,
        SinaWeibo = 3,
        RenRen = 4,
        TXWeibo = 5
    }
}
